<template>
  <div class="grower-select-component">
    <el-dialog
      title="选择农户"
      :visible.sync="dialogVisible"
      width="900px"
      append-to-body
      @closed="$emit('update:visible', false)"
    >
      <div class="filter-container">
        <el-input
          v-model="searchForm.growerName"
          placeholder="农户姓名"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleSearch"
        />
        <el-input
          v-model="searchForm.phone"
          placeholder="手机号码"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleSearch"
        />
        <el-input
          v-model="searchForm.idCard"
          placeholder="身份证号"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="handleSearch"
        />
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleSearch"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="info"
          icon="el-icon-refresh"
          @click="resetSearch"
        >重置</el-button>
      </div>

      <el-table
        v-loading="loading"
        :data="growerList"
        border
        style="width: 100%"
        @row-click="handleSelect"
      >
        <el-table-column prop="growerId" label="农户ID" width="100" />
        <el-table-column prop="growerName" label="姓名" width="120" />
        <el-table-column prop="phone" label="手机号码" width="150" />
        <el-table-column prop="idCard" label="身份证号" width="200" />
        <el-table-column prop="deptName" label="所属部门" min-width="200" />
        <el-table-column prop="address" label="地址" min-width="250" show-overflow-tooltip />
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click.stop="handleSelect(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import _ from 'lodash'

export default {
  name: 'GrowerSelect',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      growerList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      searchForm: {
        growerName: '',
        phone: '',
        idCard: ''
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.fetchGrowerList()
      }
    }
  },
  methods: {
    // 获取农户列表
    async fetchGrowerList() {
      try {
        this.loading = true
        const params = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm
        }
        const response = await request.get('/growers/page', { params })
        if (response && response.records) {
          this.growerList = response.records
          this.total = response.total
        }
      } catch (error) {
        console.error('获取农户列表失败:', error)
        this.$message.error('获取农户列表失败')
      } finally {
        this.loading = false
      }
    },

    // 处理农户选择
    handleSelect(row) {
      this.dialogVisible = false
      this.$emit('select', row)
    },

    // 处理搜索
    handleSearch: _.debounce(async function() {
      this.currentPage = 1
      await this.fetchGrowerList()
    }, 300),

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        growerName: '',
        phone: '',
        idCard: ''
      }
      this.handleSearch()
    },

    // 分页处理
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchGrowerList()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchGrowerList()
    }
  }
}
</script>

<style lang="scss" scoped>
.grower-select-component {
  .filter-container {
    margin-bottom: 20px;
  }

  .filter-item {
    margin-left: 10px;
  }

  .pagination-container {
    margin-top: 20px;
    padding: 10px 0;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style> 